
function FloatAdd(f,d){
	var c,b,a;
	try{
		c=f.toString().split(".")[1].length
	}catch(g){c=0}
	try{
		b=d.toString().split(".")[1].length
	}catch(g){b=0}
	a=Math.pow(10,Math.max(c,b));
	return(f*a+d*a)/a
}
function FloatMul(d,b){
	var a=0,
	f=d.toString(),
	c=b.toString();
	try{
		a+=f.split(".")[1].length
	}catch(g){}
	try{
		a+=c.split(".")[1].length
	}catch(g){}
	return Number(f.replace(".",""))*Number(c.replace(".",""))/Math.pow(10,a)
}

/*数字跳动效果*/
var NumerBeat  = {};
NumerBeat.add = function(o){
	var n = FloatAdd(Number(o.box.innerHTML),o.k).toFixed(0);
	if(n >= o.v1){
		clearInterval(o.interval);
		o.box.innerHTML = o.v.toString();
		return;
	};
	o.box.innerHTML  = n;
};
NumerBeat.beat = function(o){	
	if(!o.box){return};
	o.v = o.box.getAttribute("beatV");//最后的数字	
	o.v1 = Number(o.v);
	var m = parseInt(o.v1/50);	
	if(m < 1){m = 1}
	o.k = FloatMul(m,0.56).toFixed(0);	
	o.interval = setInterval(function(){NumerBeat.add(o)},20);	
};

NumerBeat.init = function(id){
	var beats = document.getElementById(id).getElementsByTagName("span");
	for(var i = 0 ; i < beats.length; i ++){
		if(beats[i].getAttribute("beatV")){	
			if(beats[i].innerHTML<1){
				NumerBeat.beat({box:beats[i]});
				$(beats[i]).attr("loaded",true);
			}else{
				beats[i].innerHTML=beats[i].getAttribute("beatV");
				$(beats[i]).attr("loaded",false);
			}
		};
	};
};
	
	

var def_pos =[
	[
		{left  : '-150%' },
		{bottom : '-300px'}
	],
	[
		{right:'-100%' },
		{left:'-100%'}
	],
	[
		{top:'-100%',right:'-100%' },
		{bottom:'-300px',left:'-100%'}
	],
	[
		{top:'-100%',left:'-100%'},
		{right:'-100%',bottom:'-300px'}
	],
	[
		{left:'-100%'},
		{bottom:'-300px'}
	]
];
var active_pos =[
	[
		{left  : 0 },
		{bottom : 0}
	],
	[
		{right:0 },
		{left:0}
	],
	[
		{top:0,right:0 },
		{bottom:0,left:0}
	],
	[
		{top:0,left:0},
		{right:0,bottom:0}
	],
	[
		{left:0},
		{bottom:0}
	]
];


var curr_step = 1, max_step=1 /*滚动页面个数*/
$(function(){
	setSize();
	for(var i in def_pos){
		$(".section").eq(i).find('.chart_section').eq(0).css(def_pos[i][0]);
		$(".section").eq(i).find('.chart_section').eq(1).css(def_pos[i][1]);
	}
	loaded();
	var i = 0;
	$('#my_elem').mousewheel(function(event, delta) { 
		if(!$("#my_elem .wrap").is(":animated")){
			clearTimeout(timer);
			//delta向下滚是-1，向上滚是1
			i +=  delta;
			if(i>=1){//向上 （4是页面个数）
				scroll_step(delta);
				i=0;
			}else if(i<=-1){//向下（4是页面个数）
				scroll_step(delta);
				i=0;
			}else{
				//如果一次没有滚动到足够的距离，则清除已滚动距离，重新开始计数
				var timer = setTimeout(function(){
					i=0;
				},700);
			}
		}
	});  
})
function scroll_step(dir){	
	if(curr_step < 1){//滚动到顶
		curr_step = 1;
		return;
	}else if(curr_step > max_step){//滚动到底
		curr_step = max_step;
		return;
	}else{
		step_deal( curr_step , dir );
		if(dir>0 && curr_step > 1){
			prev_step();
			curr_step=curr_step-1;
			$(".aside li").eq(curr_step-1).addClass("on").siblings("li").removeClass("on");
		}else if(dir<0 && curr_step != max_step){
			next_step();
			curr_step=curr_step+1;
			$(".aside li").eq(curr_step-1).addClass("on").siblings("li").removeClass("on");
		}
	}
}
function step_deal(curr_step,dir){
	switch (curr_step){
		case 1 : 
			if(dir<0){//第一步向下滚，滚动到第二步
				//page1Out();
				//page2In();
			}
			break;
		case 2 : 
			if(dir>0){//向上滚
				//page1In();
				//page2Out();
			}else{//向下滚
				//page2Out();
				//page3In();
			}
			 break;
		case 3 : 
			 if(dir>0){//向上滚
				//page2In();
				//page3Out();
			}else{//向下滚
				//page3Out();
				//page4In();
			}
			 break;
		/*case 4 : 
			 if(dir>0){//向上滚
				page3In();
				page4Out();
			}else{//向下滚
				page4Out();
				page5In();
			}
			 break;*/
		/*case 5 :
			 if(dir>0){//第五步向上滚，滚动到第四步
				page4In();
				page5Out();
			}
			 break;*/
	}
}

function next_step(){
	if(!$("#my_elem .wrap").is(":animated")){
		$("#my_elem .wrap").animate({top:'-='+($(window).height()-117)},800,'easeInOutQuart');
	}
}
function prev_step(){
	if(!$("#my_elem .wrap").is(":animated")){
		$("#my_elem .wrap").animate({top:'+='+($(window).height()-117)},800,'easeInOutQuart');
	}
}
$(window).resize(function(e) {
    setSize();
	setTimeout(function(){
		setSize()
	},10);
});
function numberMove(id){
	$("#"+id).find(".sylVal").each(function(index, element) {
		if(!$(this).attr("loaded")){
			var iLength = $(this).attr("beatV").length;
			var sInnerHtml = '';
			for(var i=0;i<iLength-1;i++){
				sInnerHtml += '0';
			}
        	$(this).text(sInnerHtml);
		}
    });
	setTimeout(function(){
		NumerBeat.init(id);
	},900);
}
function setSize(){
	var iHeight = $(window).height()-117;
	var asideH = $(".aside").innerHeight();
	$(".aside").css("marginTop",'-'+asideH/2+'px');
	$(".cbody, .wrap, .wrap > .section").height(iHeight);
}
function loaded(){
	numberMove("page1");
	$("#page1 .page_head").animate({top:0},500);
	$("#page1 .chart_section").eq(0).animate(active_pos[0][0],900);
	$("#page1 .chart_section").eq(1).animate(active_pos[0][1],1100);
}
function page1In(){
	$("#page1 .page_head").animate({top:0},500);
	$("#page1 .chart_section").eq(0).animate(active_pos[0][0],900);
	$("#page1 .chart_section").eq(1).animate(active_pos[0][1],1100);
}
function page1Out(){
	$("#page1 .page_head").animate({top:'-60px'},500);
	$("#page1 .chart_section").eq(0).animate(def_pos[0][0],900);
	$("#page1 .chart_section").eq(1).animate(def_pos[0][1],1100);
}
function page2In(){
	numberMove("page2");
	TimeId = window.setInterval("showalert(alarm_num)", 3000); 
	$("#page2 .page_head").animate({top:0},500);
	$("#page2 .chart_section").eq(0).animate(active_pos[1][0],900);
	$("#page2 .chart_section").eq(1).animate(active_pos[1][1],1100);
}
function page2Out(){
	window.clearInterval(TimeId);
	$("#page2 .page_head").animate({top:'-60px'},500);
	$("#page2 .chart_section").eq(0).animate(def_pos[1][0],900);
	$("#page2 .chart_section").eq(1).animate(def_pos[1][1],1100);
}

function page3In(){
	numberMove("page3");
	$("#page3 .page_head").animate({top:0},500);
	$("#page3 .chart_section").eq(0).animate(active_pos[2][0],900);
	$("#page3 .chart_section").eq(1).animate(active_pos[2][1],1100);
}
function page3Out(){
	$("#page3 .page_head").animate({top:'-60px'},500);
	$("#page3 .chart_section").eq(0).animate(def_pos[2][0],900);
	$("#page3 .chart_section").eq(1).animate(def_pos[2][1],1100);
}
function page4In(){
	numberMove("page4");
	$("#page4 .page_head").animate({top:0},500);
	$("#page4 .chart_section").eq(0).animate(active_pos[3][0],900);
	$("#page4 .chart_section").eq(1).animate(active_pos[3][1],900);
}
function page4Out(){
	$("#page4 .page_head").animate({top:'-60px'},500);
	$("#page4 .chart_section").eq(0).animate(def_pos[3][0],900);
	$("#page4 .chart_section").eq(1).animate(def_pos[3][1],1100);
}
function page5In(){
	numberMove("page5");
	$("#page5 .page_head").animate({top:0},500);
	$("#page5 .chart_section").eq(0).animate(active_pos[4][0],900);
	$("#page5 .chart_section").eq(1).animate(active_pos[4][1],1100);
}
function page5Out(){
	$("#page5 .page_head").animate({top:'-60px'},500);
	$("#page5 .chart_section").eq(0).animate(def_pos[4][0],900);
	$("#page5 .chart_section").eq(1).animate(def_pos[4][1],1100);
}


$(".aside li a").click(function(e) {
	if(!$("#my_elem .wrap").is(":animated")){//如果正在滚动，则点击无效
		var index = $(this).parent("li").index()+1;
		$(this).parent("li").addClass("on").siblings("li").removeClass("on");
		
		$("#my_elem .wrap").animate({top:'+='+($(window).height()-117)*(curr_step-index)},700,'easeInOutQuart');
		switch(curr_step){//前一步退出
			case (1):
				page1Out();
				break;
			case (2):
				page2Out();
				break;
			case (3):
				page3Out();
				break;
			case (4):
				page4Out();
				break;
			/*case (5):
				page5Out();
				break;*/
		}
		switch(index){//点击当前步进入
			case (1):
				page1In()
				break;
			case (2):
				page2In()
				break;
			case (3):
				page3In()
				break;
			case (4):
				page4In()
				break;
			/*case (5):
				page5In()
				break;*/
		}
		curr_step=index;//更新当前步
	}
});



